package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.Notice;
import com.sllp.C71S3TsllpShop.bean.NoticeExample;
import com.sllp.C71S3TsllpShop.bean.NoticeExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.biz.NoticeBiz;
import com.sllp.C71S3TsllpShop.dao.NoticeMapper;

@Service
public class NoticeBizImpl implements NoticeBiz {
	
	@Resource
	private NoticeMapper nm;

	@Override
	public List<Notice> findNotice(Integer who){
		NoticeExample noticeExample = new NoticeExample();
		noticeExample.createCriteria().andWhoEqualTo(who).andStateEqualTo(2);
		return nm.selectByExample(noticeExample);
	}
	
	@Override
	public List<Notice> findNotice(Notice notice,Integer pageNum,Integer pageSize){
		NoticeExample noticeExample = new NoticeExample();
		Criteria criterion = noticeExample.createCriteria();
		if(notice != null) {
			if(notice.getId() != null) {
				criterion.andIdEqualTo(notice.getId());
			}
			if(notice.getAdmin() != null && notice.getWho() != null) {
				if(pageNum != null && pageSize != null) {
					PageHelper.startPage(pageNum, pageSize);
					return nm.selectByAdminAndWho(notice);
				}
				return nm.selectByAdminAndWho(notice);
			}
			if(notice.getWho() != null) {
				criterion.andWhoEqualTo(notice.getWho());
			}
		}
		if(pageNum != null && pageSize != null) {
			PageHelper.startPage(pageNum, pageSize);
			return nm.selectByExampleWithBLOBs(noticeExample);
		}
		return nm.selectByExampleWithBLOBs(noticeExample);
	}
	
	@Override
	public Result addNotice(Notice notice) {
		int code = nm.insertSelective(notice);
		if(code > 0) {
			return new Result(1, "发布成功");
		}else {
			return new Result(0,"发布失败");
		}
	}
	
	@Override
	public Result updateNotice(Notice notice) {
		NoticeExample noticeExample = new NoticeExample();
		noticeExample.createCriteria().andIdEqualTo(notice.getId());
		int code = nm.updateByExampleSelective(notice, noticeExample);
		if(code > 0) {
			return new Result(1, "修改成功");
		}else {
			return new Result(0,"修改失败");
		}
	}
}
